export default {
  addCart(context, payload) {
    return new Promise((resolve, reject) => {
      // let oldProduct = null;
      // 判断新添加的商品是否在购物车中以存在
      /* for (let item of state.cartList) {
        if (item.iid === payload.iid) {
          oldProduct = item
        }
      } */
      // let index = state.cartList.indexOf(payload)
      let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)
        // 判断oldProduct是否有值有则等于是同一件商品则加一没有则加入整件商品
      if (oldProduct) {
        // let oldProduct = state.cartList[index];
        // oldProduct.count += 1
        context.commit('addCounter', oldProduct)
        resolve('当前商品+1');
      } else {
        payload.count = 1
          // context.state.cartList.push(payload)
        context.commit('addToCart', payload)
        resolve('收藏成功');
      }
    })
  }
}